package com.edu.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.edu.entity.Admins;
import com.edu.entity.Users;
import com.edu.service.impl.AdminServiceImpl;
import com.edu.service.impl.ArticleServiceImpl;
import com.edu.service.impl.UsersServiceImpl;
import com.edu.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class EduControllerByGyx
 */
@WebServlet("/EduControllerByGyx")
public class EduControllerByGyx extends HttpServlet {
	private static final long serialVersionUID = 1L;
	AdminServiceImpl adminService = new AdminServiceImpl();
	ArticleServiceImpl articleService = new ArticleServiceImpl();
	UsersServiceImpl userService = new UsersServiceImpl();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EduControllerByGyx() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置响应的编码格式
		response.setContentType("text/html;charset=utf-8");
		// 获取参数 参数名op的值如果等于query才做查询
		String op = request.getParameter("op");
		 		

		if ("querytopUsers".equals(op)) {
			// 查询新注册用户
			getNewUsers(request, response);
		} else if ("addAdmin".equals(op)) {
			// 二级管理员添加一级管理员--
			addAdmins(request, response);
		} else if ("usersByPage".equals(op)) {
			// 显示(根据用户名模糊查询)用户--分页效果
			getAllUsers(request, response);
		} else if ("logoutUser".equals(op)) {
			// 修改用户状态--注销(禁用)
			// 获取用户编号
			String userId = request.getParameter("userId");
			// 调用业务层
			userService.logoutUser(Integer.valueOf(userId));// 转型
			getAllUsers(request, response); // 修改完用户状态后刷新
		} else if ("startUser".equals(op)) {
			// 修改用户状态--启用
			// 获取用户编号
			String userId = request.getParameter("userId");
			userService.startUser(Integer.valueOf(userId));// 转型
			getAllUsers(request, response); // 修改完用户状态后刷新
		}else if ("adminsList".equals(op)) {
			getAdmins(request, response);
			
		}else if ("startAdmin".equals(op)) {
			//启用
			//获取管理员编号
			String adminId=request.getParameter("adminId");
			adminService.startAdmin(Integer.valueOf(adminId));//转型	
			getAdmins(request, response); //刷新
	  }else if ("logoutAdmin".equals(op)) {
			// 注销管理员
			//获取管理员编号
			String adminId=request.getParameter("adminId");
			adminService.logoutAdmin(Integer.valueOf(adminId));//转型	
			getAdmins(request, response); //刷新
     
	  }else if ("addminExit".equals(op)) {
			// 退出系统,清除session中用户的信息
			request.getSession().removeAttribute("users");
			// 重定向
			response.sendRedirect("mvc/login.jsp");
		}
	}	
	
	
	/*
	 * else { response.sendRedirect("mvc/login.jsp"); }
	 */
	private void getAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 二级管理员查询所有管理员
		//调用业务层
		List<Admins> list=adminService.getAdmins();
		Gson gson = new Gson();
		String adminsjson=gson.toJson(list);
		response.getWriter().print(adminsjson);
		response.getWriter().close();
		
	}
	private void getAllUsers(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 设置默认页码和显示条数
		int pageNo = 1;
		int pageSize = 5;
		String accountkey = "";
		// 如果有指定值就使用指定数据,否则按默认显示所有用户
		if (request.getParameter("pageNo") != null) {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		if (request.getParameter("accountKey") != null) {
			accountkey = request.getParameter("accountKey");
		}
		// 调用业务层
		PageData pageDate = userService.getUserByLikePage(pageNo, pageSize, accountkey);
		// 通过ajax返回json数据
		Gson gson = new Gson();
		String json = gson.toJson(pageDate);
		response.getWriter().print(json);
		response.getWriter().close();
	}

	private void getNewUsers(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 调用业务层
		List<Users> list = userService.getTopUser();
		Gson gson = new Gson();
		String usersjson = gson.toJson(list);
		response.getWriter().print(usersjson);
		response.getWriter().close();
	}

	private void addAdmins(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// 管理员账号
		String adminAccount = request.getParameter("adminAccount");
		// 管理员密码
		String adminPwd = request.getParameter("password");
		Admins admins = new Admins(adminAccount, adminPwd, null, 1);

		  //获取登录信息
		  HttpSession session = request.getSession(); 
		  Object obj = session.getAttribute("users");	  
		  Admins admins2=null;
		  admins2=(Admins) obj;
		// 判断登录的管理员等级----若为2级则可提交,否则没有此权限
		if (admins2.getAdminLevel() == 2) {
			// 调用业务层
			boolean flag = adminService.addAdmin(admins);
			int num = adminService.AdminsNumber();
			// 判断
			if (flag && num <= 5) {
				response.getWriter().print("<script>location.href='mvc/form-validation.jsp'</script>");
			} else {
				response.getWriter().print("<script>location.href='mvc/form-validation.jsp'</script>");
			}
		}else {
			response.getWriter().print("<script>location.href='mvc/index.jsp'</script>");
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
